package com.j1903.util;

import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.InputStream;
import java.util.UUID;

@Slf4j
public class QiNiuUtil {
    //访问Key
    private String AccessKey="JfZx2aVZUGSStmian6xI3ahfS6IZLzc5G-nTY-h8";
    //访问秘钥
    private String SecretKey="fDpeepH8bsxNrkDuV860Uqkamq8Ow8w8ztcaJdBj";
    //空间名
    private String bucketName="pingshao";
    //秘钥对象
    private Auth auth=Auth.create(AccessKey, SecretKey);
    //配置服务器区域对象
    private Zone z=Zone.autoZone();
    private Configuration config=new Configuration(z);
    //创建上传对象
    private UploadManager um=new UploadManager(config);
    private String getToken(){
        return auth.uploadToken(this.bucketName);
    }
    public String upload(InputStream is){
        String fileName= UUID.randomUUID().toString().replace("-", "")+".jsp";
        try {
            //上传图片到七牛云
            Response res=um.put(is, fileName, getToken(),null,null);
            return res.bodyString();
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
        }
        return null;
    }


}
